import { Request } from '../../../../../wxParse/request.js';
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        Administrator: 'pmliuyang003',
        statusBarHeight: getApp().globalData.statusBarHeight,
    },
    /**
     * 返回首页
     */
    goexchange: function () {
        wx.navigateBack({
            delta: 1,
        });
    },
    /**
     * 点击确认 第一次兑换先获取授权用户地址和手机
     */
    _buttonAddFriends: function () {
        // if (!this.telNumber == '' || !typeof this.telNumber == 'undefined') {
        if(!this.openGetUserAddress){
            if (!this.address == '' || !typeof this.address == 'undefined') {
                //上传收货地址
                new Request('User/UpdateAddress').request(
                    () => {
                        return {
                            openid: app.globalData.openid, //用户openid
                            userPhone: this.telNumber, //用户手机号
                            userAddress: this.address, //用户地址
                            exchangeId: this.exchangeId,        //兑换的数据库id
                        };
                    },
                    (res) => {
                        if (res.data.data) {
                            wx.showToast({
                                title: '提交成功',
                                icon: 'none',
                                duration: 2000,
                            });
                            setTimeout(() => {
                                wx.navigateBack({
                                    delta: 1,
                                });
                            }, 2001);
                        } else {
                            wx.showToast({
                                title: '提交失败',
                                icon: 'none',
                                duration: 2000,
                            });
                        }
                    }
                );
            } else {
                wx.showToast({
                    title: '请输入地址',
                    icon: 'none',
                    duration: 2000,
                });
            }
        } else {
            wx.chooseAddress({
                success: (res) => {
                    if (res.telNumber || res.cityName || res.countyName || res.detailInfo) {
                        this.telNumber = res.telNumber;
                        this.address = res.provinceName + res.cityName + res.countyName + res.detailInfo;
                        this.setData({
                            phone: this.telNumber,
                            address: this.address,
                        });
                    }
                    console.log(res);
                },
            });
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000,
            });
            if(this.phone != undefined && this.phone != '' && this.address != undefined && this.address != ''){
                this.openGetUserAddress = false;
            }
        }
    },
    /**
     * 获取手机号 值
     */
    _buttonPhone: function (params) {
        this.telNumber = params.detail.value;
    },
    /**
     * 获取地址 值
     */
    _buttonAddress: function (params) {
        this.address = params.detail.value;
    },
    /**
     * 点击复制 微信号
     */
    CopyWxHao: function () {
        wx.setClipboardData({
            data: 'pmliuyang003',
            success(res) {},
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //查询获取用户地址 如果用户已经在数据库中有记录 则不需要弹窗询问用户选择地址 直接赋值到输入框上
        this.exchangeId = options.id;
        new Request('User/GetUserAddressInfo').request(
            () => {
                return {
                    openid: app.globalData.openid, //用户openid
                };
            },
            (res) => {
                if(res.data.code == 2){
                    this.setData({
                        phone: res.data.data.userPhone,
                        address: res.data.data.userAddress,
                    });
                    this.address = res.data.data.userAddress;
                    this.telNumber = res.data.data.userPhone;
                    this.openGetUserAddress = false;
                }else if(res.data.code == 4){
                    //用户第一次输入地址 需要开启 获取地址的授权
                    this.openGetUserAddress = true;
                }
                
            }
        );
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
});
